import {
  MiddlewareConsumer,
  Module,
  NestModule,
  Req,
  RequestMethod,
} from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { CatsModule } from './cats/cats.module';
import { LoggerMiddleware } from './common/middleware/logger.middleware';
import { DatebaseModule } from './datebase/datebase.module';
import { User } from './users/user.entity';
import { UsersModule } from './users/users.module';
import { PhotosModule } from './photos/photos.module';
import { ConfigModule } from '@nestjs/config';
import { AuthModule } from './auth/auth.module';
// import { APP_GUARD } from '@nestjs/core';
// import { RolesGuard } from './auth/roles.guard';
// import { CaslModule } from './casl/casl.module';
import { GraphQLModule } from '@nestjs/graphql';
import { FooResolver } from './foo-resolver';

@Module({
  imports: [
    CatsModule,
    DatebaseModule,
    TypeOrmModule.forRoot({
      type: 'mysql',
      host: 'localhost',
      port: 3306,
      username: 'root',
      password: 'xyzj8888',
      database: 'test',
      entities: [User],
      synchronize: true,
    }),
    UsersModule,
    PhotosModule,
    ConfigModule.forRoot(),
    AuthModule,
    // CaslModule,
    GraphQLModule.forRoot({
      installSubscriptionHandlers: true,
      autoSchemaFile: 'schema.gql',
    }),
  ],
  controllers: [AppController],
  providers: [
    AppService,
    // {
    //   provide: APP_GUARD,
    //   useClass: RolesGuard,
    // },
    FooResolver,
  ],
})
export class AppModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    consumer
      .apply(LoggerMiddleware)
      // .exclude({ path: 'cats', method: RequestMethod.POST })
      .forRoutes({ path: 'cats/*', method: RequestMethod.ALL });
  }
}
